import java.util.Random;

public class Demo01 {

    public static void main(String[] args) {
        // 实现二分查找
//        System.out.println(new Random().nextInt(39) + 1);
        int[] nums = {1, 2, 3, 4, 5, 6, 29, 30, 41}; // 升序排序
        int key = 1;
        // 第一次 start - end 就是查找的范围
        int start = 0;
        int end = nums.length - 1;
        int mid = (start + end) / 2;

        while ( start <= end ) {
            if (nums[mid] > key) {
                end = mid - 1;
            }
            if (nums[mid] == key) {
                System.out.println(mid);
                break;
            }
            if (nums[mid] < key) {
                start = mid + 1;
            }
            mid = (start + end) / 2;
        }
        if (start > end) {
            System.out.println(-1);
        }
    }
}
